//package com.xjscrm.console.chat.core.impl.conversation;
//
//import com.xjscrm.console.chat.cache.CacheHolder;
//import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
//import com.xjscrm.console.chat.enums.ChatMsgType;
//import com.xjscrm.console.chat.frame.recv.RConversationCleanMsg;
//import com.xjscrm.console.chat.frame.send.SResultMsg;
//import com.xjscrm.console.chat.utils.MessageUtil;
//import com.xjscrm.console.entity.ChatTalkRecord;
//import com.xjscrm.console.entity.ChatTalkSession;
//import com.xjscrm.console.entity.ChatTalkSessionStaffRel;
//import com.xjscrm.console.service.customer.chat.ChatTalkRecordService;
//import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
//import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
//import io.netty.channel.ChannelHandlerContext;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.ObjectUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
///**
// * @author mingyang.chen create in 2024/3/17 09:42
// */
//@Component
//@Slf4j
//public class ChatConversationCleanMsgHandler extends ChatBaseMsgHandler<RConversationCleanMsg> {
//
//    @Autowired
//    private CacheHolder cacheHolder;
//
//    @Autowired
//    private ChatTalkSessionService chatTalkSessionService;
//
//    @Autowired
//    private ChatTalkRecordService chatTalkRecordService;
//
//    @Autowired
//    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;
//
//    @Override
//    public Class<RConversationCleanMsg> msgClass() {
//        return RConversationCleanMsg.class;
//    }
//
//    @Override
//    public boolean checkMsg(RConversationCleanMsg baseMsg) {
//        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getDeviceUniqueId());
//    }
//
//    @Override
//    @Transactional
//    public SResultMsg doHandle(RConversationCleanMsg content, ChannelHandlerContext ctx) {
//
//        Long merId = content.getMerId();
//        Long staffId = content.getStaffId();
//        Long deviceUniqueId = content.getDeviceUniqueId();
//
//        //
//        List<ChatTalkSessionStaffRel> chatTalkSessionStaffRels = chatTalkSessionStaffRelService.listEndByStaffId(merId, staffId);
//        if (ObjectUtils.isNotEmpty(chatTalkSessionStaffRels)) {
//            List<Long> sessionIds = chatTalkSessionStaffRels.stream().map(ChatTalkSessionStaffRel::getTalkSessionId).distinct().collect(Collectors.toList());
//            List<ChatTalkSession> sessions = chatTalkSessionService.getByIds(merId, sessionIds);
//            //
//            for (ChatTalkSession session : sessions) {
//                chatTalkSessionService.delChatRecvDelayTask(merId, staffId, session);
//                chatTalkSessionService.delChatSendDelayTask(merId, staffId, session);
//                chatTalkSessionService.delChatCleanDelayTask(merId, staffId, session);
//            }
//        } else {
//            return SResultMsg.success(content, "当前无会话可清理");
//        }
//        int count = chatTalkSessionStaffRelService.deleteByStaffIdWithEnd(merId, staffId);
//        //
//        chatTalkSessionStaffRelService.refreshStaffSessionCount(merId, staffId);
//
//        Integer staffSessionCount = cacheHolder.getStaffSessionCount(merId, staffId);
//        if (Objects.isNull(staffSessionCount)) {
//            log.info("会话清理完成, 当前员工未配置会话数量");
//            return SResultMsg.success(content, "会话清理成功");
//        }
//
//        List<Long> newTalkSessionIds = cacheHolder.setSessionOrder(content.getMerId(), deviceUniqueId, staffSessionCount);
//        if (ObjectUtils.isEmpty(newTalkSessionIds)) {
//            log.error("会话清理完成, 当前机器人没有排队的会话");
//            return SResultMsg.success(content, "会话清理成功");
//        }
//
//        List<ChatTalkSession> sessions = chatTalkSessionService.getByIds(merId, newTalkSessionIds);
//        if (ObjectUtils.isEmpty(sessions)) {
//            log.error("会话清理完成, 会话信息未找到");
//            return SResultMsg.success(content, "会话清理成功");
//        }
//        for (ChatTalkSession session : sessions) {
//
//            chatTalkSessionStaffRelService.addIfAbsent(merId, session, content.getStaffId());
//            Long lastTalkId = session.getLastRecvId();
//            ChatTalkRecord chatTalkRecord = chatTalkRecordService.getById(merId, lastTalkId);
//            MessageUtil.sendNewSessionTalk(ctx.channel(), session, chatTalkRecord);
//        }
//
//        return SResultMsg.success(content, "会话清理成功");
//    }
//
//    @Override
//    public ChatMsgType msgType() {
//        return ChatMsgType.r_conversation_clean_all;
//    }
//}
